package com.zhilun.check;

import cn.hutool.core.util.StrUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSON;
import com.zhilun.check.entity.ZhiLunPositionDataParam;

import java.util.*;
import java.util.stream.Collectors;

public class CheckPosition {


    //test
//    public static final String key ="ade0ccb442c945fb939a2f82193f6cdf";
//    public static final String secret ="MDEwMzE1MjY2MDVjMWUxOTAxNWM2N2I2OGZiYjJiYzc=";
//    public static final String url = "https://open-p.zhilunkeji.com";


    //pro
    public static final String key ="abc75a7e718a48d29cabe2884dae1d44";
    public static final String secret ="ODY2YmUzZWIwN2Y2ODU0MzIxMGY1NWFkOGQyY2YyMDY=";
    public static final String url = "https://open.zhilunkeji.com";
    public static final String BIND_API ="/open/api/wheels/opt/installSensorNo";
    public static final String UN_BIND_API ="/open/api/wheels/opt/uninstallSensorNo";


    static Map<String, String> reverseTranslationLocation = new HashMap<>();

    static {
        reverseTranslationLocation.put("L0102", "1");
        reverseTranslationLocation.put("L0101", "2");
        reverseTranslationLocation.put("R0101", "3");
        reverseTranslationLocation.put("R0102", "4");
        reverseTranslationLocation.put("L0202", "5");
        reverseTranslationLocation.put("L0201", "6");
        reverseTranslationLocation.put("R0201", "7");
        reverseTranslationLocation.put("R0202", "8");
        reverseTranslationLocation.put("L0302", "9");
        reverseTranslationLocation.put("L0301", "10");
        reverseTranslationLocation.put("R0301", "11");
        reverseTranslationLocation.put("R0302", "12");
        reverseTranslationLocation.put("L0003", "97");
        reverseTranslationLocation.put("L0001", "98");
        reverseTranslationLocation.put("L0002", "99");
    }


    static Map< Integer,String> translationLocation = new HashMap<>();
    static {
        translationLocation.put(1, "L0102");
        translationLocation.put(2, "L0101");
        translationLocation.put(3, "R0101");
        translationLocation.put(4, "R0102");
        translationLocation.put(5, "L0202");
        translationLocation.put(6, "L0201");
        translationLocation.put(7, "R0201");
        translationLocation.put(8, "R0202");
        translationLocation.put(9, "L0302");
        translationLocation.put(10, "L0301");
        translationLocation.put(11, "R0301");
        translationLocation.put(12, "R0302");
        translationLocation.put(97, "L0003");
        translationLocation.put(98, "L0001");
        translationLocation.put(99, "L0002");
    }

    public static void main(String[] args) throws InterruptedException {


        String filePath = "C:\\Users\\hcf\\Desktop\\大车队长车牌终端.xlsx";  // 替换为实际文件路径
        String filePath2 = "C:\\Users\\hcf\\Desktop\\大车知轮主机绑定-11-28.xlsx";  // 替换为实际文件路径

        // 读取 Excel 文件
        //知轮数据
        List<ZhiLunPositionDataParam> zhilunList = EasyExcel.read(filePath)
                .head(ZhiLunPositionDataParam.class)
                .sheet().headRowNumber(1)
                .doReadSync();

        //大车数据
        List<ZhiLunPositionDataParam> dacheList = EasyExcel.read(filePath2)
                .head(ZhiLunPositionDataParam.class)
                .sheet().headRowNumber(1)
                .doReadSync();

        List<ZhiLunPositionDataParam>  dataList  = new ArrayList<>();
        for (ZhiLunPositionDataParam data : zhilunList) {
            try {
                if (StrUtil.isBlank(data.getDeviceId()) ||StrUtil.isBlank(data.getLicensePlate())) {
                    continue;
                }
                data.setLicensePlate(data.getLicensePlate().trim());
                data.setDeviceId(data.getDeviceId().trim());
                dataList.add(data);

            }catch (Exception e){
                System.out.println(JSON.toJSONString(data));
            }

        }

        dacheList.addAll(dataList);










        // 按 licensePlate 和 location 分组
        // 按 licensePlate 和 location 分组
        Map<String, List<ZhiLunPositionDataParam>> groupedData = dacheList.stream()
                .collect(Collectors.groupingBy(
                        ZhiLunPositionDataParam::getLicensePlate  // 拼接成字符串作为组合键
                ));

        // 对每个分组内的 List 进行时间筛选，得到每个分组中时间最新的记录

        List<ZhiLunPositionDataParam> resultList = new ArrayList<>();
        for (String key : groupedData.keySet()) {
            List<ZhiLunPositionDataParam> ZhiLunPositionDataParams = groupedData.get(key);
            if (ZhiLunPositionDataParams.size() >2) {
                System.out.println("超过3条绑定,key: "+ key);
                continue;
            }
            if (ZhiLunPositionDataParams.size() ==1){
                resultList.add(ZhiLunPositionDataParams.get(0));
            }
            if (ZhiLunPositionDataParams.size() ==2){
                if (ZhiLunPositionDataParams.get(0).equals(ZhiLunPositionDataParams.get(1))) {
                    continue;
                }
                ZhiLunPositionDataParam latestData = ZhiLunPositionDataParams.stream()
                        .max(Comparator.comparing(ZhiLunPositionDataParam::getDate)) // 根据 date 字段获取最新的记录
                        .orElse(null);

                resultList.add(latestData);

            }

        }

        Map<String, List<ZhiLunPositionDataParam>> resultListMap = resultList.stream()
                .collect(Collectors.groupingBy(
                        ZhiLunPositionDataParam::getSource  // 拼接成字符串作为组合键
                ));

        for (String key : resultListMap.keySet()) {

            List<ZhiLunPositionDataParam> ZhiLunPositionDataParams = resultListMap.get(key);
            String fileName ="主机-" + key +".xlsx";
            EasyExcel.write(fileName, ZhiLunPositionDataParam.class)
                    .sheet("Sheet1")
                    .doWrite(ZhiLunPositionDataParams);
            System.out.println(JSON.toJSONString(ZhiLunPositionDataParams));
        }






    }






}
